public class demo1{
    int []dx={0,0,1,-1};
    int m,n;
    int []dy={1,-1,0,0};
    int x;
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        m=image.length;
        n=image[0].length;
        x=image[sr][sc];
        if(x==color)return image; //不处理边界情况会形成死递归
        //  image[sr][sc]=color;
        dfs(image,sr,sc,color);
        return image;
    }
    public void dfs(int[][] image,int i,int j,int color){
        image[i][j]=color;
        for(int k=0;k<4;k++){
            int a=i+dx[k];
            int b=j+dy[k];
            if(a>=0&&a<m&&b>=0&&b<n&&image[a][b]==x){
                dfs(image,a,b,color);
            }
        }
        return;
    }
}
